{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f290b9df",
   "metadata": {},
   "source": [
    "# NumPy多维索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aabe5c1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "##keep\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa7bfc47",
   "metadata": {},
   "outputs": [],
   "source": [
    "##keep\n",
    "a = np.arange(10).reshape(2, 5)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63a6a40a",
   "metadata": {},
   "source": [
    "## 定位单个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a1f08f6",
   "metadata": {},
   "source": [
    "访问第0行："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60b578d0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "65df0867",
   "metadata": {},
   "source": [
    "访问第1行第1列的元素，使用两重下标的写法:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0406ae34",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c2755cea",
   "metadata": {},
   "source": [
    "访问第1行第1列的元素，使用NumPy写法:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd8f1779",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "352dd019",
   "metadata": {},
   "source": [
    "访问第1行第1列的元素，使用元组的写法:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c29e1c84",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "97e6cd6b",
   "metadata": {},
   "source": [
    "## 多维切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "079c881f",
   "metadata": {},
   "source": [
    "复制数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ad4cd9d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b4484b13",
   "metadata": {},
   "source": [
    "选出偶数列组成的二维数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c409ae9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a1dc2dfc",
   "metadata": {},
   "source": [
    "选出第1行："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2417d834",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "616b5457",
   "metadata": {},
   "source": [
    "选出第2列："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df340f86",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6dbe9ab0",
   "metadata": {},
   "source": [
    "## np.newaxis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4df51e4e",
   "metadata": {},
   "source": [
    "newaxis即为None:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "797334ea",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e07a80cd",
   "metadata": {},
   "source": [
    "确认数组a的维度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5985388",
   "metadata": {},
   "outputs": [],
   "source": [
    "##keep\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "647d39b8",
   "metadata": {},
   "source": [
    "将newaxis放在最后（新数组命名为b）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "344a7bec",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "8d2e61b3",
   "metadata": {},
   "source": [
    "确认新生成的数组的维度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c08a53f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "49f67819",
   "metadata": {},
   "source": [
    "将newaxis放在形状的中间（新数组命名为c）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c6466bf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "4917d8d7",
   "metadata": {},
   "source": [
    "确认新的数组的维度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c044144",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a9f48fc4",
   "metadata": {},
   "source": [
    "将newaxis放在最开始的位置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6291719",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b213c47b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "855210d2",
   "metadata": {},
   "source": [
    "## Ellipsis\n",
    "选择多维数组元素的语法糖。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78b8948d",
   "metadata": {},
   "source": [
    "用Ellipsis选出第0列："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3033ca8a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f0af6332",
   "metadata": {},
   "source": [
    "选出第0列，使用切片语法的等价写法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43e03042",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c999fbd9",
   "metadata": {},
   "source": [
    "去掉b数组新增加的维度，Ellipsis语法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b906ceb4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b91f22b1",
   "metadata": {},
   "source": [
    "去掉b数组新增加的维度，切片的语法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9c86e7a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9e19770a",
   "metadata": {},
   "source": [
    "## 整数数组索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8943417a",
   "metadata": {},
   "outputs": [],
   "source": [
    "##keep\n",
    "x = np.arange(25).reshape(5, 5)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad9de1a9",
   "metadata": {},
   "source": [
    "选出对角线元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95e0346e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6d22d136",
   "metadata": {},
   "source": [
    "选出对角元素，使用arange："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3be28c95",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e1961fdf",
   "metadata": {},
   "source": [
    "选出四个角上的元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64a529df",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d4a43569",
   "metadata": {},
   "source": [
    "使用ix_函数，选出四个角上的元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aeb96777",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "1f2e1460",
   "metadata": {},
   "source": [
    "判断下面的表达式的输出是什么？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6f07253",
   "metadata": {},
   "outputs": [],
   "source": [
    "##keep\n",
    "x[np.ix_([0,1,2,3,4], [3,4])]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
